import {
	getHttpHostApi,
	getHttpRequestAllApi,
} from '@utils/apis'

const colors = ['#0077aa', '#00a8c6', '#0081c2', '#0001C0', '#004488', '#00a8aa', '#003bb3', '#01a0e4', '#004488'];

let _graphMinBar = null
let _graphHourBar = null
let _graphDayBar = null
let _httpOrgMinData = null
let _httpOrgHourData = null
let _httpOrgDayData = null

module.exports = {

	init: async function() {
		let {
			minYkeys,
			hourYkeys,
			dayYkeys,
		} = await getHttpHostApi()
		minYkeys = minYkeys.toString().split(",")
		hourYkeys = hourYkeys.toString().split(",")
		dayYkeys = dayYkeys.toString().split(",")
		let options = {
			element: 'httpd-access-min',
			data: [],
			xkey: 'xKey',
			ykeys: minYkeys,
			labels: minYkeys,
			xLabels: "60sec",
			xLabelAngle: 30,
			xLabelMargin: 0,
			hideHover: true,
			resize: true,
			horizontal: false,
			stacked: true,
			barColors: colors,
			units: '',
		}
		_graphMinBar = Morris.Bar(options)
		options.element = 'httpd-access-hour'
		options.xLabels = "hour"
		options.units = "w"
		options.ykeys = hourYkeys
		options.labels = hourYkeys
		delete options.horizontal
		_graphHourBar = Morris.Bar(options)
		options.element = 'httpd-access-day'
		options.xLabels = "day"
		options.ykeys = dayYkeys
		options.labels = dayYkeys
		_graphDayBar = Morris.Bar(options)
		_httpOrgMinData = null
		_httpOrgHourData = null
		_httpOrgDayData = null
	},

	loadHttpDatas(params = {}) {
		let {
			interval_min = 2,
				interval_hour = 2,
				interval_day = 2,
		} = params
		return getHttpRequestAllApi({
			min: interval_min,
			hour: interval_hour,
			day: interval_day,
		}).then(result => {
			let {
				min: newMinData,
				hour: newHourData,
				day: newDayData,
			} = result
			let minData = _httpOrgMinData || newMinData || []
			let hourData = _httpOrgHourData || newHourData || []
			let dayData = _httpOrgDayData || newDayData || []
			if (_httpOrgMinData && _httpOrgMinData.length > 0) {
				minData.unshift()
				minData.unshift()
				minData.push(newMinData[0], newMinData[1])
			}
			if (_httpOrgHourData && _httpOrgHourData.length > 0) {
				hourData.unshift()
				hourData.unshift()
				hourData.push(newHourData[0], newHourData[1])
			}
			if (_httpOrgDayData && _httpOrgDayData.length > 0) {
				dayData.unshift()
				dayData.unshift()
				dayData.push(newDayData[0], newDayData[1])
			}
			_graphMinBar.setData(minData);
			_graphHourBar.setData(hourData);
			_graphDayBar.setData(dayData);
			let minCount = 0;
			let hourCount = 0.0;
			let dayCount = 0.0;
			let md = newMinData[newMinData.length - 1]
			let hd = newHourData[newHourData.length - 1]
			let dd = newDayData[newDayData.length - 1]
			for (let key in md)
				if (key != 'xKey')
					minCount += parseInt(md[key]) || 0;
			for (let key in hd)
				if (key != 'xKey')
					hourCount += parseFloat(hd[key]) || 0.0;
			hourCount = parseInt(hourCount / 10000);
			for (let key in dd)
				if (key != 'xKey')
					dayCount += parseFloat(dd[key]) || 0.0;
			dayCount = parseInt(dayCount / 10000);
			return {
				minCount,
				hourCount,
				dayCount,
			}
		})
	},
}